<?php
namespace Admin\Controller;
use Think\PageAdmin;
use Common\Support\Curl;
use Think\Upload;
use Common\Support\Validator;

/**
 * 教师管理控制器
 */
class TeacherController extends BaseController
{

    /**
     * 初始化
     * 
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();       
    }

    public function index(){
        $prefix = C('DB_PREFIX');

        $count = M('teacher')->count();
        $page  = new PageAdmin($count, C('PAGE_NUM'));
        
        $pages['show']  = $page->show();  //分页输出
        $pages['total'] = $count;  //总数
        $pages['lists'] = M('teacher a')
            ->order("a.id desc")
            ->limit($page->firstRow.','.$page->listRows)
            ->select();

        $this->seotitle = '艺术家列表';
        $this->pages    = $pages;
        $this->display();
    }

    public function add(){
        $this->seotitle = "添加艺术家";
        $this->display();
    }

    /**
     * 保存
     * @return [type] [description]
     */
    public function save()
    {
        $action = I('get.action', '', 'trim');
        $params = I('post.');

        if($params['mobile'] == ''){
            $this->error('请输入用户名/手机号');
        }

        if($params['nickname'] == ''){
            $this->error('请输入昵称');
        }

        if($params['password'] == ''){
            $this->error('请输入密码');
        }

        if($params['type'] == '0'){
            $this->error('请选择类别');
        }

        $file = M('files')->where(['file_id'=>$params['headimg']])->find();
        if($file){
            $file_path = C('DOMAIN').$file['filepath'];
        }else{
            $file_path = $params['headimg'];
        }
        

        // 新增
        if ($action == 'add') {
            M('teacher')->add([
                'mobile'        => trim($params['mobile']),
                'nickname'      => $params['nickname'],
                'password'      => $params['password'],
                'headimg'       => $file_path,
                'status'        => $params['status'] == 1 ? 1 : 0,
                'type'          => $params['type'],
                'remark'        => $params['remark'],
                'addtime'       => time(),
                'updatetime'    => time()
            ]);

            $this->success('操作成功', U('index'));
        }

        // 编辑保存
        elseif ($action == 'edit') {
            $id = I('post.id', 0, 'intval');            

            M('teacher')->where(['id' => $id])->save([
                'mobile'        => trim($params['mobile']),
                'nickname'      => $params['nickname'],
                'password'      => $params['password'],
                'headimg'       => $file_path,
                'status'        => $params['status'] == 1 ? 1 : 0,
                'type'          => $params['type'],
                'remark'        => $params['remark'],
                'updatetime'    => time()
            ]);

            $this->success('操作成功', U('index'));
        }
    }

    public function edit(){
        $id = I('get.id', 0, 'intval');

        if (empty($id) ||
            ! $info = M('teacher')->where(['id' => $id])->find())
        {
            $this->error('参数错误');
        }

        $this->seotitle  = '艺术家编辑';
        $this->info      = $info;
        $this->display();
    }

    public function delete(){
        $id = I('get.id', 0, 'intval');

        M('teacher')->where(['id' => $id])->delete();

        $this->success('操作成功');
    }

    

}